/*
 * Copyright 2010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yannickstucki.android.musicqueue.playback;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.communication.INotificator;
import com.yannickstucki.android.musicqueue.data.Song;

/**
 * A class to display notifications in the status bar about the current playing
 * song.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class Notificator implements INotificator {

  /**
   * The index used in the notification which shows the current playing song.
   */
  private static final int NOTIFICATION_INDEX = 1;

  /**
   * Used to display notifications in the top bar.
   */
  private NotificationManager notificationManager;

  /**
   * Used to create the notification.
   */
  private PendingIntent intent;

  /**
   * The context is needed to create notifications.
   */
  private Context context;

  /**
   * The constructor sets the references.
   */
  public Notificator(Context context,
      NotificationManager notificationManager, PendingIntent intent) {
    this.notificationManager = notificationManager;
    this.intent = intent;
    this.context = context;
  }

  @Override
  public void cancelNotification() {
    notificationManager.cancel(NOTIFICATION_INDEX);
  }

  @Override
  public void showNotification(Song song) {
    Notification notification = new Notification(R.drawable.play, context
        .getString(R.string.playing)
        + " " + song.title, System.currentTimeMillis());

    notification.setLatestEventInfo(context, "Music Queue", song.title + " - " + song.artist,
        intent);

    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notificationManager.notify(NOTIFICATION_INDEX, notification);
  }
}
